/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OutputSelection.java
 *
 * Created on Apr 12, 2011, 9:21:26 AM
 */

package project3;

import java.util.*;
import javax.xml.stream.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Kevin
 */
public class OutputSelection extends javax.swing.JFrame {

    private int optionValue;
    ReportGenerators reportGenerator;
    Timestamp startDate;
    Timestamp endDate;
    SavingWindow loadingScreen;

    /** Creates new form OutputSelection */
    public OutputSelection(int optionValue, Timestamp startDate, Timestamp endDate) {
        initComponents();
        reportGenerator = new ReportGenerators();
        this.optionValue = optionValue;
        this.startDate = startDate;
        this.endDate = endDate;
        loadingScreen = new SavingWindow();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        txtRadio = new javax.swing.JRadioButton();
        xmlRadio = new javax.swing.JRadioButton();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Select Print Type:");

        buttonGroup1.add(txtRadio);
        txtRadio.setSelected(true);
        txtRadio.setText(".txt File");

        buttonGroup1.add(xmlRadio);
        xmlRadio.setText(".xml File");

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(xmlRadio)
                    .addComponent(txtRadio)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                        .addGap(10, 10, 10)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(txtRadio)
                .addGap(18, 18, 18)
                .addComponent(xmlRadio)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        JFileChooser fc = new JFileChooser(".");


        if(txtRadio.isSelected()){
            FileFilter filter = new ExtensionFileFilter("txt", new String[] { "txt"});
            fc.setFileFilter(filter);
            int returnVal = fc.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = new File(fc.getSelectedFile().getAbsolutePath() + ".txt");
                loadingScreen.setVisible(true);
                if(optionValue == 1){
                    reportGenerator.createOptionOneTextFileReport(file, startDate, endDate);
                }
                else if(optionValue == 2){
                    reportGenerator.createOptionTwoTextFileReport(file, startDate, endDate);
                }
                else{
                    reportGenerator.createOptionThreeTextFileReport(file, startDate, endDate);
                }
                loadingScreen.dispose();
                this.dispose();
            }
        }
        else{
            FileFilter filter = new ExtensionFileFilter("xml", new String[] { "xml"});
            fc.setFileFilter(filter);
            int returnVal = fc.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                String file = (fc.getSelectedFile().getAbsolutePath() + ".xml");
                loadingScreen.setVisible(true);
                this.setVisible(false);
                if(optionValue == 1){
                    reportGenerator.createOptionOneXMLReport(file, startDate, endDate);
                }
                else if(optionValue == 2){
                    reportGenerator.createOptionTwoXMLReport(file, startDate, endDate);
                }
                else{
                    reportGenerator.createOptionThreeXMLReport(file, startDate, endDate);
                }
                loadingScreen.dispose();
                this.dispose();
            }
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                java.sql.Timestamp start = java.sql.Timestamp.valueOf("1960-04-06 00:00:00");
                java.sql.Timestamp end = java.sql.Timestamp.valueOf("2011-04-06 00:00:00");
                new OutputSelection(1, start, end).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton okButton;
    private javax.swing.JRadioButton txtRadio;
    private javax.swing.JRadioButton xmlRadio;
    // End of variables declaration//GEN-END:variables

}
